package com.physics.mps.module.comsol.builder.geom;

import com.physics.mps.module.comsol.builder.Builder;
import com.physics.mps.module.comsol.vo.CoordinateVo;
import com.physics.mps.module.comsol.vo.GeomVo;
import com.physics.mps.module.comsol.vo.PairVo;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author mcsoft
 * @date 2023/10/16 11:23
 * @description comsol java 导入几何脚本构建
 */
public class GeomBuilder {

    private final Builder builder;

    public GeomBuilder(Builder builder) {
        this.builder = builder;
    }


    public void build(String geomTag, String filename, GeomVo geomVo, List<PairVo> pairVos) {
        builder.echo("Geom");
        // 创建组件几何
        builder.change(1, 2);
        builder.append("GeomSequence geom = comp.geom().create(");
        builder.value(geomTag);
        builder.append(", 3);");

        // 导入stp文件并构建
        impAndRun(filename, geomVo);

        // 创建一致边界对
        createPair(geomTag, pairVos);
    }

    /**
     * 导入stp文件并构建
     * @param filename
     * @param geomVo
     */
    private void impAndRun(String filename, GeomVo geomVo) {
        if (geomVo == null) {
            return;
        }

        String impTag = builder.uniqueTag("imp");
        builder.change(1, 2);
        builder.append("GeomFeature geomFeature = geom.create(");
        builder.value(impTag);
        builder.append(", \"Import\");");

        builder.change(1, 2);
        builder.append("geomFeature.set(\"type\", \"cad\");");

        builder.change(1, 2);
        builder.append("geomFeature.set(\"filename\", ");
        // 文件绝对路径
        builder.value(filename);
        builder.append(");");

        builder.change(1, 2);
        builder.append("geomFeature.importData();");

        builder.change(1, 2);
        builder.append("geomFeature.set(\"importtol\", ");
        builder.value(geomVo.getImportTol());
        builder.append(");");

        builder.change(1, 2);

        // 联合体
        if ("union".equals(geomVo.getAction())) {
            builder.append("geom.feature(\"fin\").set(\"action\", \"union\");");
            builder.append("geom.feature(\"fin\").set(\"repairtoltype\", \"auto\");");
        } else {
            builder.append("geom.feature(\"fin\").set(\"action\", \"assembly\");");
            builder.change(1, 2);
            builder.append("geom.feature(\"fin\").set(\"repairtoltype\", \"relative\");");
            builder.change(1, 2);
            builder.append("geom.feature(\"fin\").set(\"repairtol\", ");
            builder.append(geomVo.getRepairTol());
            builder.append(");");
        }
        builder.change(1, 2);
        builder.append("geom.feature(\"fin\").set(\"createpairs\", ");
        builder.append(geomVo.isCreatePairs());
        builder.append(");");

        builder.change(1, 2);
        builder.append("geom.run(");
        builder.value(impTag);
        builder.append(");");
    }


    /**
     * 创建一致边界对
     * @param pairVos
     */
    private void createPair(String geomTag, List<PairVo> pairVos){
        if (CollectionUtils.isEmpty(pairVos)) {
            return;
        }
        for (PairVo pairVo : pairVos) {
            String pairTag = builder.uniqueTag("p");
            String pairObjTag = builder.uniqueTag("pair");
            // 创建选择
            CoordinateVo source = pairVo.getSource();
            CoordinateVo destination = pairVo.getDestination();
            String sourceSelect = builder.createBallSelect(2, source);
            String destinationSelect = builder.createBallSelect(2, destination);

            builder.change(2, 2);
            builder.append("Pair ");
            builder.append(pairObjTag);
            builder.append(" = comp.pair().create(");
            builder.value(pairTag);
            builder.append(", \"Identity\");");

            builder.change(1, 2);
            builder.append(pairObjTag);
            builder.append(".source().named(");
            builder.value(geomTag + "_" + sourceSelect);
            builder.append(");");

            builder.change(1, 2);
            builder.append(pairObjTag);
            builder.append(".destination().named(");
            builder.value(geomTag + "_" + destinationSelect);
            builder.append(");");

            builder.change(1, 2);
            builder.append(pairObjTag);
            builder.append(".manualDist(true);");

            builder.change(1, 2);
            builder.append(pairObjTag);
            builder.append(".searchDist(\"1e-2\");");
        }
    }


}
